package com.w2e.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.w2e.common.utils.Result;
import com.w2e.user.entity.Follow;
import com.w2e.user.mapper.FollowMapper;
import com.w2e.user.service.IFollowService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyyl
 * @since 2024-06-26
 */
@RestController
@RequestMapping("/follows")
@RequiredArgsConstructor
public class FollowController {

    private final FollowMapper followMapper;

    private final IFollowService followService;

    @PostMapping
    public Result follow(@RequestBody Follow follow){
        LambdaQueryWrapper<Follow> lqw = new LambdaQueryWrapper();
        lqw.eq(Follow::getFanId,follow.getFanId());
        lqw.eq(Follow::getFollowedId,follow.getFollowedId());
        Follow f = followMapper.selectOne(lqw);

        if (f == null){
            follow.setFollowDate(LocalDateTime.now());
            follow.setFlag(true);
            followMapper.insert(follow);
        }else {
            follow.setFlag(true);
            followMapper.update(follow,lqw);
        }

        return new Result(200,null,"");
    }

    @PutMapping
    public Result noFollow(@RequestBody Follow follow){
        follow.setFollowDate(LocalDateTime.now());
        follow.setFlag(false);
        LambdaQueryWrapper<Follow> lqw = new LambdaQueryWrapper();
        lqw.eq(Follow::getFanId,follow.getFanId());
        lqw.eq(Follow::getFollowedId,follow.getFollowedId());
        followMapper.update(follow,lqw);
        return new Result(200,null,"");
    }

    @GetMapping("isFollow")
    public Result isFollow(Follow follow){
        LambdaQueryWrapper<Follow> lqw = new LambdaQueryWrapper();
        lqw.eq(Follow::getFanId,follow.getFanId());
        lqw.eq(Follow::getFollowedId,follow.getFollowedId());
        Follow f = followMapper.selectOne(lqw);
        if (f == null){
            return new Result(200,false,"");
        }
        return new Result(200,f.getFlag(),"");
    }


}
